<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
cfc('core')->checklogin();
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();
$uniacid=$_SESSION['uniacid'];
$mid=$_SESSION['uid'];
$basic=$this->plugin_setting('basic');
$return['basic']=$basic;
if($_RQ['op']=='main'){
	if(!$_RQ['id']){
		exi('没有找到该活动，请重新选择！','error','/index');
	}
	if(!$_RQ['help_mid']){
		$wota='自己';
		$member=pdo_get('core_members',array('uniacid'=>$uniacid,'id'=>$mid));
		//从玩家表查询玩家数据
		$player=pdo_get('jy_yousetli_player',array('uniacid'=>$uniacid,'mid'=>$mid,'actid'=>$_RQ['id']));
		//帮忙的数据
		$helpdata=pdo_getall('jy_yousetli_help',array('uniacid'=>$uniacid,'actid'=>$_RQ['id'],'help_mid'=>$mid));
	}else{
		$wota='他';
		$help_mid=$_RQ['help_mid'];
		$member=pdo_get('core_members',array('uniacid'=>$uniacid,'id'=>$_RQ['help_mid']));
		//从玩家表查询玩家数据
		$player=pdo_get('jy_yousetli_player',array('uniacid'=>$uniacid,'mid'=>$_RQ['help_mid'],'actid'=>$_RQ['id']));
		//帮忙的数据
		$helpdata=pdo_getall('jy_yousetli_help',array('uniacid'=>$uniacid,'actid'=>$_RQ['id'],'help_mid'=>$help_mid));
		
	}
	$act=pdo_get('jy_yousetli_act',array('uniacid'=>$uniacid,'id'=>$_RQ['id']));
	//判断游戏状态
	if($act['starttime']>time()){
		$zhuangtai=1;
	}else if($act['endtime']<time()){
		$zhuangtai=3;	
	}else{
		$zhuangtai=2;	
	}
	
	if(isset($player['id'])){

	}else{
		$insert=array(
			'uniacid'=>$uniacid,
			'mid'=>$mid,
			'actid'=>$_RQ['id'],
			'createtime'=>TIMESTAMP,
			
		);
		pdo_insert('jy_yousetli_player',$insert);
	}
	$act['createtime']=date('Y-m-d H:i:s',$act['createtime']);
	$act['endtimestr']=date('Y-m-d H:i',$act['endtime']);
	//判断玩家的剩余游戏次数
	if(!$help_mid){
		$cishu=$act['my_count']-$player['play_times']+$player['share_times'];
	}else{
		$help=pdo_get('jy_yousetli_help',array('uniacid'=>$uniacid,'mid'=>$mid,'actid'=>$_RQ['id'],'help_mid'=>$help_mid));
		$cishu=$act['friends_count']-$help['play_times'];
	}
	
	if($cishu > 0){
		$cishu_text = '帮'.$wota.'抢优惠('.$cishu.')次';
		$cishu_url = 'jy_yousetli/game//id='.$act['id'].'&help_mid='.$help_mid;
	}else{
		$cishu_text = '次数已用完';
		$cishu_url = "javascript:;";
	}
	
	//每次游戏获得的优惠
	if($helpdata){
		foreach($helpdata as &$data){
			$data['xiangqing']=explode(";",$data['xiangqing']);
		}
	}
	//计算总优惠
	if($help_mid>0){
		$sql = "SELECT SUM(money) FROM " . tablename('jy_yousetli_help') . " WHERE uniacid=:uniacid AND actid=:id AND help_mid=:help_mid";
		$summoney= pdo_fetch($sql,array(":uniacid" => $_SESSION['uniacid'],":id" => $_RQ['id'],":help_mid" => $help_mid));	
		$sum =$summoney['SUM(money)'];
	}else{	
		$sql = "SELECT SUM(money) FROM " . tablename('jy_yousetli_help') . " WHERE uniacid=:uniacid AND actid=:id AND help_mid=:help_mid";
		$summoney= pdo_fetch($sql,array(":uniacid" => $_SESSION['uniacid'],":id" => $_RQ['id'],":help_mid" => $mid));	
		$sum =$summoney['SUM(money)'];
	}
	$return['player']=$player;
	$return['act']=$act;
	$return['zhuangtai']=$zhuangtai;
	$return['endtimestr']=$endtimestr;
	$return['member']=$member;
	$return['cishu']=$cishu;
	$return['cishu_text']=$cishu_text;
	$return['cishu_url']=$cishu_url;
	$return['helpdata']=$helpdata;
	$return['wota']=$wota;
	$return['help_mid']=$help_mid?:0;
	$return['helps_sum']=$sum;
	$return['sharedata']=array(
		'title'=>$act['title'],
		'desc'=>'我正在参加'.$act['title'].'活动，快来帮我拿优惠',
		'link'=>custom_url('jy_yousetli/detail//id='.$_RQ['id'].'&help_mid='.$mid,true),
		'imgUrl'=>ATTACHMENT_URL.$act['image'],
	);
	set_html('title',$act['title']);
	exi($return);
}